package com.wandian.weather.mvvm.othercity;

import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.LocalCityBean;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.eventbus.ChangeMessage;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.CityDataUtils;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.R;
import com.wandian.weather.adapter.WeatherCityRecycleAdapter;
import com.wandian.weather.databinding.WeatherActivityOtherCityBinding;

import org.greenrobot.eventbus.EventBus;

import static com.mylib.libcore.config.AppSPKeyConfig.CITY_NAME;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 其他城市
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_WEATHER_OTHER_CITY)
public class WeatherOtherCityActivity extends BaseMVVMActivity<WeatherOtherCityViewModel> {
    private WeatherActivityOtherCityBinding dataBinding;
    private WeatherCityRecycleAdapter weatherCityRecycleAdapter;
    private String cityName="";
    private String cityCode="";
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.weather_activity_other_city);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initClink();
        initViewData();
        initRecycle();
        if(UIUtils.isEmpty(CityDataUtils.getLocationList())) CityDataUtils.getLocalCityJson(this);
        AppContext.getInstance().getAppPreferencesHelper().setKeyValue("select_city","");
        AppContext.getInstance().getAppPreferencesHelper().setKeyValue("select_address","");
    }

    private void initRecycle(){
        dataBinding.recycler.post(new Runnable() {
            @Override
            public void run() {
                CityDataUtils.getLocalCityJson(WeatherOtherCityActivity.this);
            }
        });
        weatherCityRecycleAdapter = new WeatherCityRecycleAdapter(R.layout.weather_item_city);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,3);
        dataBinding.recycler.setLayoutManager(layoutManager);
        dataBinding.recycler.setAdapter(weatherCityRecycleAdapter);

        weatherCityRecycleAdapter.setNewData(CityDataUtils.getListProvince());
        weatherCityRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                LocalCityBean cityBean = (LocalCityBean)baseQuickAdapter.getData().get(i);
                if(!UIUtils.isEmpty(cityBean)){
                    cityName = cityBean.getName();
                    cityCode = cityBean.getCode();
                    weatherCityRecycleAdapter.notifySelectItemView(cityBean.getName());
                    dataBinding.tvNowCity.setText(cityName);
                }
            }
        });

    }

    private void initViewData(){
        cityName = AppContext.getInstance().getAppPreferencesHelper().getKeyValue(CITY_NAME);
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("当前-"+cityName);
        dataBinding.bindTransparentToolBar.toolBarTitleRight.setText("切换");
        dataBinding.bindTransparentToolBar.toolBarTitleRight.setVisibility(View.VISIBLE);
    }

    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v ->finish());
        //其他区域
        dataBinding.tvCityOtherAddress.setOnClickListener(v ->{
            if(!UIUtils.isEmpty(cityCode)){
                Bundle bundle = new Bundle();
                bundle.putString("cityName",cityName+"");
                bundle.putString("cityCode",cityCode+"");
                ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_WEATHER_CITY_CHILD).with(bundle).navigation();
            }else {
                ToastUtil.show("请选择您要切换的城市");
            }
        });
        //切换
        dataBinding.bindTransparentToolBar.toolBarTitleRight.setOnClickListener(v -> getAddressWeather());

    }

    private void getAddressWeather(){
        if(!UIUtils.isEmpty(cityCode)){
            AppContext.getInstance().getAppPreferencesHelper().setKeyValue("select_city",cityName);
        }else {
            AppContext.getInstance().getAppPreferencesHelper().setKeyValue("select_city","");
        }
        AppContext.getInstance().getAppPreferencesHelper().setKeyValue("select_address","");
        EventBus.getDefault().post(new ChangeMessage("changeWeatherCity"));
        finish();
    }

}
